/*
 * Copyright (c) 2017-present, CV4J Contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.core.filters;

import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.core.datamodel.lut.LUT;

import java.util.Optional;

/**
 * 滤色器
 *
 * @author dev
 * @date 2021/07/22
 */
public class ColorFilter extends BaseFilter {
    /**
     * 秋天的风格
     */
    public static final int AUTUMN_STYLE = 0;
    /**
     * 骨风格
     */
    public static final int BONE_STYLE = 1;
    /**
     * 很酷的风格
     */
    public static final int COOL_STYLE = 2;
    /**
     * 热的风格
     */
    public static final int HOT_STYLE = 3;
    /**
     * hsv风格
     */
    public static final int HSV_STYLE = 4;
    /**
     * 飞机的风格
     */
    public static final int JET_STYLE = 5;
    /**
     * 海洋风格
     */
    public static final int OCEAN_STYLE = 6;
    /**
     * 粉红色的风格
     */
    public static final int PINK_STYLE = 7;
    /**
     * 彩虹的风格
     */
    public static final int RAINBOW_STYLE = 8;
    /**
     * 春天的风格
     */
    public static final int SPRING_STYLE = 9;
    /**
     * 夏天的风格
     */
    public static final int SUMMER_STYLE = 10;
    /**
     * 冬天的风格
     */
    public static final int WINTER_STYLE = 11;

    /**
     * 风格
     */
    private int style;

    /**
     * 滤色器
     */
    public ColorFilter() {
        style = AUTUMN_STYLE;
    }

    /**
     * 设置风格
     *
     * @param style 风格
     */
    public void setStyle(int style) {
        this.style = style;
    }

    /**
     * 做的过滤器
     *
     * @param src src
     * @return {@link ImageProcessor}
     */
    @Override
    public Optional<ImageProcessor> doFilter(ImageProcessor src) {
        int tr = 0;
        int tg = 0;
        int tb = 0;
        int[][] lut = getStyleLUT(style);
        int size = RED.length;
        for (int i = 0; i < size; i++) {
            tr = RED[i] & 0xff;
            tg = GREED[i] & 0xff;
            tb = BLUE[i] & 0xff;

            RED[i] = (byte) lut[tr][0];
            GREED[i] = (byte) lut[tg][1];
            BLUE[i] = (byte) lut[tb][2];
        }
        return Optional.ofNullable(src);
    }

    /**
     * 得到风格附近地区
     *
     * @param style 风格
     * @return {@link int[][]}
     */
    private int[][] getStyleLUT(int style) {
        return LUT.getColorFilterLUT(style);
    }
}
